package com.hry.job;

import com.hry.config.DingProperty;
import com.hry.config.SpringContextHolder;
import com.hry.enums.JobDataMapKey;
import com.hry.po.Tcustom;
import com.hry.po.User;
import com.hry.service.TcustomService;
import com.hry.util.DingUtil;
import com.hry.util.HryUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.TriggerKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

/**
 * @Description: 执行定制测试, 具体执行哪个定制, 由JobDataMap传CustomId决定
 * @Author: luqiwei
 * @Date: 2018/12/12 13:22
 */

@Slf4j
public class RunCustomJob extends QuartzJobBean {
    @Autowired
    DingProperty dingProperty;

    @Override
    protected void executeInternal(JobExecutionContext context) {

        JobDataMap jobDataMap = context.getMergedJobDataMap();

        Integer customId = Integer.valueOf(jobDataMap.getString(JobDataMapKey.CUSTOM_ID.getKey()));//定制ID
        Integer executeUserId = Integer.valueOf(jobDataMap.getString(JobDataMapKey.EXECUTE_USER_ID.getKey()));//执行用户ID
        String executeUserName = jobDataMap.getString(JobDataMapKey.EXECUTE_USER_NAME.getKey());//执行用户RealName
        TriggerKey triggerKey = context.getTrigger().getKey();
        if (!ObjectUtils.allNotNull(customId, executeUserId, executeUserName)) {
            log.error("triggerName=" + triggerKey + ",triggerGroup=" + triggerKey.getGroup());
            log.error("JobDataMap入参有误,需要customId,executeUserId,executeUserName缺一不可");
            return;
        }
        User executeUser = new User();
        executeUser.setId(executeUserId);
        executeUser.setRealname(executeUserName);

        TcustomService tcustomService = SpringContextHolder.getBean(TcustomService.class);
        String reportName = tcustomService.run(customId, executeUser, true);
        log.info("triggerName=" + triggerKey.getName() + ",triggerGroup="
                + triggerKey.getGroup() + ",成功触发了定制Id=" + customId + "的执行");


        //判断是否发送钉钉消息
        Tcustom tcustom = tcustomService.selectOne(customId);
        if(tcustom.getIsSendMessage() == 1){
            String reportURl = "http://" + HryUtil.getInet4Address() +":8885" + reportName;
            log.info("定制测试报告地址为：" + reportURl);

            String dingUrl = dingProperty.getDingUrl();
            log.info("取到的dingUrl="+dingUrl);
            Boolean isAtAll = dingProperty.getAtAll();
            String content = "触发方式：定时任务触发\n自动化测试运行时间："+HryUtil.getCurrentTime()+"\n自动化测试定制名称：" + tcustom.getCustomname()+ "\n" + dingProperty.getContent() + "：" + reportURl + "\n请点击链接查看接口自动化测试报告！";
            String[] list = dingProperty.getMobileList();
            //延时发送测试报告钉钉消息
            try {
                Thread.sleep(dingProperty.getWaitTime());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            DingUtil dingUtil = new DingUtil();
            dingUtil.sendDingReprot(dingUrl,isAtAll,content,list);


        }


    }
}
